﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.ComponentModel;
using System.Timers;

namespace ForfiterStockInfo.Client
{
    class TimedHTMLAdapter
    {
        private Timer _timer;
        private WebClient _webClient = new WebClient();
        private string _uri;

        public TimedHTMLAdapter(double timerInterval, string uri)
        {
            _uri = uri;
            _timer = new Timer(timerInterval);
            _webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
            _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
        }

        public event DownloadStringCompletedEventHandler DownloadCompleted
        {
            add { _webClient.DownloadStringCompleted += value; }
            remove { _webClient.DownloadStringCompleted -= value; }
        }

        public void GetHTMLSource()
        {
            try
            {
                _webClient.DownloadStringAsync(new Uri(_uri));
            }
            catch (Exception ex)
            {
                // do nothing.
            }
        }

        public void Start()
        {
            _timer.Start();
        }

        public void Stop()
        {
            _timer.Stop();
        }

        private void WebClient_DownloadStringCompleted(object sedner, DownloadStringCompletedEventArgs e)
        {

        }

        private void Timer_Elapsed(object sedner, ElapsedEventArgs e)
        {
            if (!_webClient.IsBusy)
                GetHTMLSource();
        }
    }
}
